#include "Authentication.h"

#include <util/StringUtils.h>

Authentication::Authentication() :
	type(IQ_TYPE_SET)
{
}


void setDigest(std::string & connectionID, std::string & password) {
	this->digest = StringUtils::hash(connectionID + password);
}


std::string Authentication::getChildElementXML() {
	std::string buf("<query xmlns=\"jabber:iq:auth\">");
	if (!username.empty()) {
		buf.append("<username>").append(username).append("</username>");
	} else {
		buf.append("<username/>")
	}

	if (!digest.empty()) {
		buf.append("<digest>").append(digest).append("</digest>");
	} else {
		buf.append("<digest/>")
	}

	if (!password.empty() && !digest.empty()) {
		buf.append("<password>").append(StringUtils::escapeForXML(password)).append("</password>");
	} else {
		buf.append("<password/>")
	}

	if (!resource.empty()) {
		buf.append("<resource>").append(resource).append("</resource>");
	} else {
		buf.append("<resource/>")
	}

	buf.append("</query>");
	return buf;
}

